home *** CD-ROM | disk | FTP | other *** search
- package de.trantor.mail.demo.j2me;
-
- import de.trantor.mail.ImapClient;
- import de.trantor.mail.InboxClient;
- import de.trantor.mail.Message;
- import de.trantor.mail.MimeDecoder;
- import de.trantor.mail.Pop3Client;
- import de.trantor.mail.Pop3Exception;
- import de.trantor.mail.SmtpClient;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.util.Vector;
- import javax.microedition.lcdui.Alert;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Image;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
- import javax.microedition.rms.RecordStore;
- import javax.microedition.rms.RecordStoreException;
-
- public class MailMIDlet extends MIDlet implements CommandListener {
- private Display display = Display.getDisplay(this);
- private SetupScreen setupScreen = new SetupScreen(this);
- private InboxScreen inboxScreen = new InboxScreen(this);
- private ReadScreen readScreen = new ReadScreen(this);
- private WriteScreen writeScreen = new WriteScreen(this);
- private String address = "yourname@yourisp.com";
- private String hostname = "yourhost.yourisp.com";
- private String pop3Host = "pop.yourisp.com";
- private String pop3User = "your-username";
- private String pop3Pass = "your-password";
- private String smtpHost = "smtp.yourisp.com";
- private boolean debug = false;
- private boolean imap = false;
- private InboxClient pop3Client;
- private Vector msgNumbers = new Vector();
-
- public MailMIDlet() {
- try {
- this.loadSetupData();
- } catch (Exception var2) {
- ((Throwable)var2).printStackTrace();
- }
-
- this.setupScreen.setAddress(this.address);
- this.setupScreen.setHostname(this.hostname);
- this.setupScreen.setPop3Host(this.pop3Host);
- this.setupScreen.setPop3User(this.pop3User);
- this.setupScreen.setPop3Pass(this.pop3Pass);
- this.setupScreen.setSmtpHost(this.smtpHost);
- this.setupScreen.setDebug(this.debug);
- this.setupScreen.setImap(this.imap);
- }
-
- public void commandAction(Command cmd, Displayable dsp) {
- System.out.println("Command: " + cmd.getLabel());
-
- try {
- if (cmd == SetupScreen.OK) {
- this.address = this.setupScreen.getAddress();
- this.hostname = this.setupScreen.getHostname();
- this.pop3Host = this.setupScreen.getPop3Host();
- this.pop3User = this.setupScreen.getPop3User();
- this.pop3Pass = this.setupScreen.getPop3Pass();
- this.smtpHost = this.setupScreen.getSmtpHost();
- this.debug = this.setupScreen.getDebug();
- this.imap = this.setupScreen.getImap();
-
- try {
- this.saveSetupData();
- } catch (Exception var12) {
- ((Throwable)var12).printStackTrace();
- }
-
- if (this.imap) {
- this.pop3Client = new ImapClient();
- } else {
- this.pop3Client = new Pop3Client();
- }
-
- this.pop3Client.setDebug(this.debug);
- this.pop3Client.open(this.pop3Host, this.pop3User, this.pop3Pass);
- this.getMessageList();
- this.display.setCurrent(this.inboxScreen);
- } else if (cmd == InboxScreen.READ) {
- int num = this.inboxScreen.getMessageIndex();
- if (num == -1) {
- return;
- }
-
- Message message = this.pop3Client.getMessage((Integer)this.msgNumbers.elementAt(num));
- this.readScreen.setSender(Message.getMachineAddress(message.getHeaderValue("From", "No sender")));
- this.readScreen.setDate(message.getHeaderValue("Date", "No date"));
- this.readScreen.setSubject(message.getHeaderValue("Subject", "No subject"));
- this.readScreen.clearBody();
- MimeDecoder mime = new MimeDecoder(message);
- this.addPartToScreen(mime);
- this.display.setCurrent(this.readScreen);
- } else if (cmd == InboxScreen.WRITE) {
- this.writeScreen.clear();
- this.display.setCurrent(this.writeScreen);
- } else if (cmd == InboxScreen.DELETE) {
- int num = this.inboxScreen.getMessageIndex();
- if (num == -1) {
- return;
- }
-
- this.pop3Client.removeMessage((Integer)this.msgNumbers.elementAt(num));
- this.inboxScreen.delete(this.inboxScreen.getMessageIndex());
- this.msgNumbers.removeElementAt(num);
- this.display.setCurrent(this.inboxScreen);
- } else if (cmd == InboxScreen.EXIT) {
- this.destroyApp(false);
- ((MIDlet)this).notifyDestroyed();
- } else if (cmd == ReadScreen.OK) {
- this.display.setCurrent(this.inboxScreen);
- } else if (cmd == WriteScreen.OK) {
- Message message = new Message(this.address, this.writeScreen.getRecipient(), this.writeScreen.getSubject());
- message.addBodyLine(this.writeScreen.getBody());
- SmtpClient smtpClient = new SmtpClient(this.hostname);
-
- try {
- smtpClient.setDebug(this.debug);
- smtpClient.open(this.smtpHost);
- smtpClient.sendMessage(message);
- } finally {
- smtpClient.close();
- }
-
- this.display.setCurrent(this.inboxScreen);
- } else if (cmd == WriteScreen.CANCEL) {
- this.display.setCurrent(this.inboxScreen);
- }
- } catch (Exception var13) {
- Alert alert = new Alert("Error");
- alert.setString(var13.getClass().getName() + ": " + ((Throwable)var13).getMessage());
- ((Throwable)var13).printStackTrace();
- this.display.setCurrent(alert, this.setupScreen);
- }
-
- }
-
- protected void startApp() throws MIDletStateChangeException {
- this.display.setCurrent(this.setupScreen);
- }
-
- protected void destroyApp(boolean unconditional) {
- try {
- this.pop3Client.close();
- } catch (Exception var3) {
- }
-
- }
-
- protected void pauseApp() {
- }
-
- private void saveSetupData() throws RecordStoreException, IOException {
- try {
- RecordStore.deleteRecordStore("mail4me");
- } catch (RecordStoreException var9) {
- }
-
- RecordStore store = RecordStore.openRecordStore("mail4me", true);
-
- try {
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- DataOutputStream output = new DataOutputStream(buffer);
- output.writeUTF(this.address);
- output.writeUTF(this.hostname);
- output.writeUTF(this.pop3Host);
- output.writeUTF(this.pop3User);
- output.writeUTF(this.pop3Pass);
- output.writeUTF(this.smtpHost);
- output.writeBoolean(this.debug);
- output.writeBoolean(this.imap);
- store.addRecord(buffer.toByteArray(), 0, buffer.size());
- } finally {
- store.closeRecordStore();
- }
-
- }
-
- private void loadSetupData() throws RecordStoreException, IOException {
- try {
- RecordStore store = RecordStore.openRecordStore("mail4me", false);
-
- try {
- ByteArrayInputStream buffer = new ByteArrayInputStream(store.getRecord(1));
- DataInputStream input = new DataInputStream(buffer);
- this.address = input.readUTF();
- this.hostname = input.readUTF();
- this.pop3Host = input.readUTF();
- this.pop3User = input.readUTF();
- this.pop3Pass = input.readUTF();
- this.smtpHost = input.readUTF();
- this.debug = input.readBoolean();
- this.imap = input.readBoolean();
- } finally {
- store.closeRecordStore();
- }
- } catch (Exception var9) {
- }
-
- }
-
- private void getMessageList() throws Pop3Exception, IOException {
- Vector list = new Vector();
- int count = this.pop3Client.getMessageCount();
-
- for(int i = 0; i < count; ++i) {
- Message message = this.pop3Client.getHeaders(i);
- list.addElement(message.getHeaderValue("Subject", "No subject") + " (" + Message.getMachineAddress(message.getHeaderValue("From", "No sender") + ")"));
- this.msgNumbers.insertElementAt(new Integer(i), 0);
- }
-
- this.inboxScreen.setMessages(list);
- }
-
- private void addPartToScreen(MimeDecoder mime) {
- if (mime.getPartCount() == 0) {
- if (mime.getType().equals("image/png")) {
- byte[] bytes = mime.getBodyBytes();
- this.readScreen.addImage(Image.createImage(bytes, 0, bytes.length));
- } else if (mime.getType() != null && !mime.getType().equals("text/plain")) {
- this.readScreen.addBody("\n[Unable to display \"" + mime.getType() + "\" part.]");
- } else {
- String s = "";
-
- for(int i = 0; i < mime.getBodyLineCount(); ++i) {
- s = s + "\n" + mime.getBodyLine(i);
- }
-
- this.readScreen.addBody(s);
- }
- } else {
- for(int p = 0; p < mime.getPartCount(); ++p) {
- this.addPartToScreen(mime.getPart(p));
- }
- }
-
- }
- }
-